<?php
/**
 * Created by PhpStorm.
 * User: kun
 * Date: 23/6/19
 * Time: 20:37
 */

namespace app\common\model;

use think\Model;

class Jpush extends Base
{

    /**
     * 根据来获取列表的数据
     * @param array $condition
     * @param int $from
     * @param int $size
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getNewsByCondition($condition = [], $from = 0, $size = 5)
    {
        if (!isset($condition['status'])) {
            $condition['status'] = [
                'neq', config('code.status_delete')
            ];
        }

        $order = ['id' => 'desc'];
        $result = $this->where($condition)
            ->field($this->_getListFiled())
            ->limit($from, $size)
            ->order($order)
            ->select();

//        echo $this->getLastSql(); //输出sql语句

        return $result;
    }

    /**
     * 根据条件来获取列表的数据的总数
     * @param array $condition
     * @return int|string
     */
    public function getNewsCountByCondition($condition = [])
    {
        if (!isset($condition['status'])) {
            $condition['status'] = [
                'neq', config('code.status_delete')
            ];
        }
        $result = $this->where($condition)->count();

//        echo $this->getLastSql(); //输出sql语句

        return $result;
    }


    /**
     * 通用化获取参数的数据字段
     */
    private function _getListFiled()
    {
        return [
            'id',
            'title',
            'status',
            'app_type',
            'news_id',
            'catid',
            'update_time',
            'create_time',
        ];
    }


}